package com.czu.cy.pages.order;

import java.util.Date;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.czu.cy.dal.CrudServiceDAO;
import com.czu.cy.entities.Order;
import com.czu.cy.pages.Home;
import com.czu.cy.services.Authenticator;

public class CreateOrder {

    @Property
    @Persist
    private Order order;

    @Inject
    private Authenticator authenticator;
    
    @Inject
    private CrudServiceDAO crudServiceDAO;

    @Log
    public void onActivate()
    {
        order = new Order();
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "orderForm")
    public Object createOrder()
    {
        order.setUser(authenticator.getLoggedUser());
        order.setCreationDate(new Date());
        crudServiceDAO.create(order);
        return Home.class;
    }

    @OnEvent(value = "cancelOrder")
    @Log
    public Object cancelOrder()
    {
        order = null;
        return Home.class;
    }

    @OnEvent(value = EventConstants.VALIDATE, component = "orderForm")
    public void validateOrder()
    {

    }

}
